package org.zhadoop.stream.cql.semanticanalyzer.parser.context;

import java.util.List;

import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.executor.BinaryExpression;
import org.zhadoop.stream.cql.executor.BinaryExpressionRegistry;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.expressiondesc.BinaryExpressionDesc;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.expressiondesc.ExpressionDescribe;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextreplacer.ParseContextReplacer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

/**
 * 逻辑表达式语Or 语法解析内容
 * 
 * @author zwd
 *
 */
public class LogicExpressionOrContext extends BaseExpressionParseContext {

	private List<BaseExpressionParseContext> expressions;

	public LogicExpressionOrContext() {
		expressions = Lists.newArrayList();
	}

	public List<BaseExpressionParseContext> getExpressions() {
		return expressions;
	}

	public void setExpressions(List<BaseExpressionParseContext> expressions) {
		this.expressions = expressions;
	}
	
	@Override
	public String toString() {
		return Joiner.on(" OR ").join(expressions);
	}
	
	@Override
	protected void walkChild(ParseContextWalker walker) {
		walkExpressions(walker, expressions);
	}
	

	@Override
	public void walkChildAndReplace(ParseContextReplacer replacer) {
		List<Integer> repalcedIndex = foundIndexsInChilds(replacer);
		replace(repalcedIndex,replacer);
	}

	private void replace(List<Integer> repalcedIndex, ParseContextReplacer replacer) {
		BaseExpressionParseContext replacedContext = replacer.createReplaceParseContext();
		for(Integer index : repalcedIndex){
			expressions.set(index, replacedContext);
		}
	}

	private List<Integer> foundIndexsInChilds(ParseContextReplacer replacer) {
		List<Integer> replacedIndex = Lists.newArrayList();
		for(int i = 0; i < expressions.size(); i++){
			BaseExpressionParseContext child = expressions.get(i);
			if(replacer.isChildsReplaceable(child)){
				replacedIndex.add(i);
			}else{
				child.walkChildAndReplace(replacer);
			}
		}
		return replacedIndex;
	}

	@Override
	public ExpressionDescribe createExpressionDesc() throws SemanticAnalyzerException {
		if(expressions.size() == 1){
			return expressions.get(0).createExpressionDesc(getSchemas());
		}
		return createBinaryExpression();
	}

	/**
	 * 创建binary 表达式
	 * logicExpressionAnd (KW_OR logicExpressionAnd)*
	 * 从后往前计算表达式
	 * @return
	 */
	private ExpressionDescribe createBinaryExpression() throws SemanticAnalyzerException{
		BinaryExpression bexp = BinaryExpressionRegistry.getBinaryExpressionByName("or");
		BinaryExpressionDesc bexpdesc = new BinaryExpressionDesc(bexp);
		
		List<BaseExpressionParseContext> rightArguments = Lists.newLinkedList();
		rightArguments.addAll(expressions);
		return null;
	}



	

}
